﻿using System;
using System.Collections.Generic;
using System.Text;
using Lswweb.Model;
using Lswweb.Domain;
using Lswweb.IDAL;

namespace Lswweb.Domain
{
    public class UpFiles
    {
        private static IUpFiles fileDAL = DALFactory.CreateUpFilesDAL();

        #region 查询

        public static UpFileInfo GetFile(int id)
        {
            if (id <= 0)
                return null;

            return fileDAL.GetFile(id);
        }

        public static IList<UpFileInfo> GetFiles(string searchKey, int categoryId, StatusType? status, int pageIndex, int pageSize, out int totalRecord)
        {
            return fileDAL.GetFiles(searchKey, categoryId, SiteContext.Current.Language,status, pageIndex, pageSize, out totalRecord);
        }

        #endregion

        #region 添加/删除

        public static void Insert(UpFileInfo info)
        {
            info = FileHandler(info);
            int upFileId=fileDAL.Insert(info);
            if (upFileId > 0)
            {
                info.Id = upFileId;
                SiteContext.RenderMessage(ExceptionType.HandleSuccess);
            }
            else
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError);
            }
        }

        public static void Delete(int id)
        {
            string returnUrl = SiteContext.Current.ReferrerUrl;
            if (id <= 0)
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError, returnUrl);
                return;
            }

            ExceptionType type = ExceptionType.UnknownError;
            string fileUrl = (string)fileDAL.GetFieldValue(id,"FileUrl");
            Globals.FileDelete(fileUrl);
            if (fileDAL.Delete(id))
                type = ExceptionType.HandleSuccess;
            SiteContext.RenderMessage(type, returnUrl);
        }

        public static void Update(UpFileInfo info)
        {
            if (info == null)
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError);
            }

            UpFileInfo oldInfo = GetFile(info.Id);
            if (!string.IsNullOrEmpty(info.FileUrl) && info.FileUrl != oldInfo.FileUrl)
                Globals.FileDelete(oldInfo.FileUrl);
            else if (info.FileUrl == oldInfo.FileUrl)
                info.FileSize = oldInfo.FileSize;

            info = FileHandler(info);            

            if (fileDAL.Update(info))
                SiteContext.RenderMessage(ExceptionType.HandleSuccess);
            else
                SiteContext.RenderMessage(ExceptionType.UnknownError);
            
        }

        #endregion

        private static UpFileInfo FileHandler(UpFileInfo info)
        {
            if (info == null)
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError);
                return null;
            }

            if (info.Id <= 0 && SiteContext.Current.Id > 0)
                info.Id = SiteContext.Current.Id;

            info.FileUrl = ImageHelper.MoveImagesToDirectory(SiteModule.UpFile, info.FileUrl);

            if (string.IsNullOrEmpty(info.Ver))
                info.Ver = SiteContext.Current.Language;

            return info;
        }
    }

    public class UpFileConst
    {
        /// <summary>
        /// 首页轮换图类别Id
        /// </summary>
        public const int DefaultFlashNodeId = 1;

        /// <summary>
        /// 首页促销活动Id
        /// </summary>
        public const int DefaultSalesPromotionId = 3;

        /// <summary>
        /// 首页新品上架
        /// </summary>
        public const int DefaultNewProduct1Id = 4;
        public const int DefaultNewProduct2Id = 5;
        public const int DefaultNewproduct3Id = 6;

        public const int DefaultDV1 = 7;
        public const int DefaultDV2 = 8;
        public const int DefaultDV3 = 9;

        public const int DefaultBanner1 = 10;
        public const int DefaultBanner2 = 11;
        public const int DefaultBanner3 = 12;

        public const int InnerLeft1 = 13;
        public const int InnerLeft2 = 14;
    }
}
